package view;

import javax.management.modelmbean.ModelMBean;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Point;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;

import model.Crime_typeModel;
import model.FIRModel;
import model.HistoryModel;
import model.criminalModel;
import model.userModel;

import com.toedter.calendar.JDateChooser;
import common.loadData;

import dao.FIRDAO;
import dao.HistoryDao;
import dao.crime_typeDao;
import dao.criminalDAO;
import dao.encrypt;
import dao.userdao;

public class addFir extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel panelTitle = null;
	private JLabel lbtitle = null;
	private JLabel LabelTitle = null;
	private JLabel LabelChooseCriminal = null;
	private JLabel LabelChooseHistory = null;
	private JLabel LabelChooseCrimeType = null;
	private JLabel LabelStatus = null;
	private JLabel LabelDateTemp = null;
	private JLabel LabelDateStart = null;
	private JLabel LabelDateEnd = null;
	private JTextField txtNameTitle = null;
	private JComboBox ComboBoxCrimial = null;
	private JComboBox ComboBoxHistory = null;
	private JComboBox ComboBoxCrimeType = null;
	private JComboBox ComboBoxStatus = null;
	private JButton btnsave = null;
	private JButton btnExit = null;
	private JDateChooser txtDateTemp;
	private JDateChooser txtDateStart;
	private JDateChooser txtDateEnd;
	private JLabel lbtime = null;
	private JTextField txtTime = null;

	public addFir() {
		super();
		initialize();
		loadDataByID();
	}

	private void initialize() {
		this.setSize(466, 542);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/image/List.png")));
		this.setName("addFir");
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("Add FIR");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lbtime = new JLabel();
			lbtime.setBounds(new Rectangle(44, 283, 81, 24));
			lbtime.setText("Time:");
			LabelDateEnd = new JLabel();
			LabelDateEnd.setBounds(new Rectangle(44, 414, 117, 28));
			LabelDateEnd.setText("Date End :");
			LabelDateStart = new JLabel();
			LabelDateStart.setBounds(new Rectangle(43, 366, 118, 30));
			LabelDateStart.setText("Date Start:");
			LabelDateTemp = new JLabel();
			LabelDateTemp.setBounds(new Rectangle(44, 323, 117, 29));
			LabelDateTemp.setText("Date Temp:");
			LabelStatus = new JLabel();
			LabelStatus.setBounds(new Rectangle(43, 241, 119, 27));
			LabelStatus.setText("Status:");
			LabelChooseCrimeType = new JLabel();
			LabelChooseCrimeType.setBounds(new Rectangle(45, 195, 118, 30));
			LabelChooseCrimeType.setText("Choose Crime Type:");
			LabelChooseHistory = new JLabel();
			LabelChooseHistory.setBounds(new Rectangle(45, 150, 91, 29));
			LabelChooseHistory.setText("Choose History:");
			LabelChooseCriminal = new JLabel();
			LabelChooseCriminal.setBounds(new Rectangle(44, 106, 96, 29));
			LabelChooseCriminal.setText("Choose Criminal:");
			LabelTitle = new JLabel();
			LabelTitle.setBounds(new Rectangle(45, 60, 75, 30));
			LabelTitle.setText("Title Name:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelTitle(), null);
			jContentPane.add(LabelTitle, null);
			jContentPane.add(LabelChooseCriminal, null);
			jContentPane.add(LabelChooseHistory, null);
			jContentPane.add(LabelChooseCrimeType, null);
			jContentPane.add(LabelStatus, null);
			jContentPane.add(LabelDateTemp, null);
			jContentPane.add(LabelDateStart, null);
			jContentPane.add(LabelDateEnd, null);
			jContentPane.add(getTxtNameTitle(), null);
			jContentPane.add(getComboBoxCrimial(), null);
			jContentPane.add(getComboBoxHistory(), null);
			jContentPane.add(getComboBoxCrimeType(), null);
			jContentPane.add(getComboBoxStatus(), null);
			jContentPane.add(getBtnsave(), null);
			jContentPane.add(getBtnExit(), null);
			jContentPane.add(getTxtDateTemp(), null);
			jContentPane.add(getTxtDateStart(), null);
			jContentPane.add(getTxtDateEnd(), null);
			jContentPane.add(lbtime, null);
			jContentPane.add(getTxtTime(), null);
		}
		return jContentPane;
	}

	private JPanel getPanelTitle() {
		if (panelTitle == null) {
			lbtitle = new JLabel();
			lbtitle.setLocation(new Point(0, 0));
			lbtitle.setFont(new Font("Dialog", Font.BOLD, 18));
			lbtitle.setForeground(new Color(31, 122, 122));
			lbtitle.setText("Add FIR Report");
			lbtitle.setSize(new Dimension(139, 30));
			panelTitle = new JPanel();
			panelTitle.setLayout(null);
			panelTitle.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(0, 0, 2, 0, Color.red), "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			panelTitle.setSize(new Dimension(138, 31));
			panelTitle.setLocation(new Point(160, 16));
			panelTitle.add(lbtitle, null);
		}
		return panelTitle;
	}

	private JTextField getTxtNameTitle() {
		if (txtNameTitle == null) {
			txtNameTitle = new JTextField();
			txtNameTitle.setSize(new Dimension(211, 26));
			txtNameTitle.setLocation(new Point(194, 64));
		}
		return txtNameTitle;
	}

	private JComboBox getComboBoxCrimial() {
		if (ComboBoxCrimial == null) {
			ComboBoxCrimial = new JComboBox();
			ComboBoxCrimial.setLocation(new Point(195, 107));
			ComboBoxCrimial.setSize(new Dimension(211, 26));
			ArrayList<criminalModel> crime = criminalDAO.getAllCrime();
			for(criminalModel model : crime)
			{
				ComboBoxCrimial.addItem(model.getFirstName()+" "+model.getLastName());
			}
		}
		return ComboBoxCrimial;
	}

	private JComboBox getComboBoxHistory() {
		if (ComboBoxHistory == null) {
			ComboBoxHistory = new JComboBox();
			ComboBoxHistory.setLocation(new Point(195, 151));
			ComboBoxHistory.setSize(new Dimension(211, 26));
			ArrayList<HistoryModel> his = HistoryDao.getALL();
			for(HistoryModel model : his)
			{
				ComboBoxHistory.addItem(model.getName());
			}
		}
		return ComboBoxHistory;
	}

	private JComboBox getComboBoxCrimeType() {
		if (ComboBoxCrimeType == null) {
			ComboBoxCrimeType = new JComboBox();
			ComboBoxCrimeType.setLocation(new Point(195, 196));
			ComboBoxCrimeType.setSize(new Dimension(211, 26));
			ArrayList<Crime_typeModel> type = crime_typeDao.getAll();
			for(Crime_typeModel model : type)
			{
				ComboBoxCrimeType.addItem(model.getName());
			}
		}
		return ComboBoxCrimeType;
	}

	private JComboBox getComboBoxStatus() {
		if (ComboBoxStatus == null) {
			ComboBoxStatus = new JComboBox();
			ComboBoxStatus.setLocation(new Point(194, 245));
			ComboBoxStatus.setSize(new Dimension(211, 26));
			ComboBoxStatus.addItem("Select Status");
			ComboBoxStatus.addItem("Criminal");
			ComboBoxStatus.addItem("Prisoner");
			ComboBoxStatus.addItem("Most Wanted");
			ComboBoxStatus.setSelectedIndex(0);
			ComboBoxStatus.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					if(ComboBoxStatus.getSelectedItem().toString().equals("Criminal") || ComboBoxStatus.getSelectedItem().toString().equals("Most Wanted"))
					{
						txtDateTemp.setEnabled(false);
						txtDateStart.setEnabled(false);
						txtDateEnd.setEnabled(false);
						txtTime.setEditable(false);
						txtTime.setText("0");
					}
					else
					{
						txtDateTemp.setEnabled(true);
						txtDateStart.setEnabled(true);
						txtDateEnd.setEnabled(true);
						txtTime.setEditable(true);
					}
				}
			});
			
		}
		return ComboBoxStatus;
	}

	private JButton getBtnsave() {
		if (btnsave == null) {
			btnsave = new JButton();
			btnsave.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnsave.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
			btnsave.setText("Save");
			btnsave.setPreferredSize(new Dimension(70, 34));
			btnsave.setSize(new Dimension(91, 32));
			btnsave.setLocation(new Point(191, 463));
			btnsave.setHorizontalAlignment(SwingConstants.LEFT);
			btnsave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// validate
				    	 FIRModel model = new FIRModel();
							if(panelFIR.IdFIR>=0)
							{
								model.setId(panelFIR.IdFIR);
							}
							model.setIdcriminal(FIRDAO.getid_Crime(ComboBoxCrimial.getSelectedItem().toString()));
							model.setIdcrim_Type(crime_typeDao.getid_Crime_type(ComboBoxCrimeType.getSelectedItem().toString()));
							model.setTitle_Name(txtNameTitle.getText());
							if(ComboBoxStatus.getSelectedItem().toString().equals("1"))
							{
								model.setStatus_crime(0);
							}else if(ComboBoxStatus.getSelectedItem().toString().equals("2"))
							{
								model.setStatus_crime(1);
							}else if(ComboBoxStatus.getSelectedItem().toString().equals("3"))
							{
								model.setStatus_crime(2);
							}
							model.setTime(Integer.parseInt(txtTime.getText()));
							SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
							String dtem = sdf.format(txtDateTemp.getDate());
							String dstart = sdf.format(txtDateTemp.getDate());
							String dend = sdf.format(txtDateTemp.getDate());
							model.setDate_Temp(dtem);
							model.setDate_Start(dstart);
							model.setDate_End(dend);
							java.util.Date now = new java.util.Date();
							String d = sdf.format(now);
							model.setLast_Update(d);
							// process add User
							if(panelFIR.IdFIR>=0)
							{
								int rs = FIRDAO.updatefir(model);
								if(rs==1)
								{
									JOptionPane.showMessageDialog(null, "Update sucessful!");
									loadData.dataFir();
									if(login.idrole==0)
										loadData.tab=6;
									else
										loadData.tab=5;
								
								}
								else
								{
									JOptionPane.showMessageDialog(null, "Update failed!");
									if(login.idrole==0)
										loadData.tab=6;
									else
										loadData.tab=5;
								
								}
							}
							else
								{
									int rs = FIRDAO.insertFIR(model);
									if(rs==1)
									{
										JOptionPane.showMessageDialog(null, "Insert sucessful!");
										loadData.dataFir();
										if(login.idrole==0)
											loadData.tab=6;
										else
											loadData.tab=5;
								
									}
									else
									{
										JOptionPane.showMessageDialog(null, "Insert failed!");
										if(login.idrole==0)
											loadData.tab=6;
										else
											loadData.tab=5;
										
									}
							}
							mainForm a = new mainForm();
							a.setVisible(true);
							dispose();
				}	
			});
		}
		return btnsave;
	}

	private JButton getBtnExit() {
		if (btnExit == null) {
			btnExit = new JButton();
			btnExit.setHorizontalTextPosition(SwingConstants.RIGHT);
			btnExit.setIcon(new ImageIcon(getClass().getResource("/image/Close.png")));
			btnExit.setText("Exit");
			btnExit.setLocation(new Point(313, 463));
			btnExit.setSize(new Dimension(91, 32));
			btnExit.setHorizontalAlignment(SwingConstants.LEFT);
			btnExit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(login.idrole==0)
						loadData.tab=6;
					else
						loadData.tab=5;
					mainForm a = new mainForm();
					a.setVisible(true);
					dispose();
				}
			});
		}
		return btnExit;
	}

	private JDateChooser getTxtDateTemp() {
		if (txtDateTemp == null) {
			txtDateTemp = new JDateChooser();
			txtDateTemp.setDateFormatString("MM/dd/yyyy");
			txtDateTemp.setLocation(new Point(196, 329));
			txtDateTemp.setSize(new Dimension(211, 26));
			txtDateTemp.getDateEditor().setEnabled(false);
		}
		return txtDateTemp;
	}

	private JDateChooser getTxtDateStart() {
		if (txtDateStart == null) {
			txtDateStart = new JDateChooser();
			txtDateStart.setDateFormatString("MM/dd/yyyy");
			txtDateStart.setSize(new Dimension(211, 26));
			txtDateStart.setLocation(new Point(196, 372));
			txtDateStart.getDateEditor().setEnabled(false);
		}
		return txtDateStart;
	}

	private JDateChooser getTxtDateEnd() {
		if (txtDateEnd == null) {
			txtDateEnd = new JDateChooser();
			txtDateEnd.setDateFormatString("MM/dd/yyyy");
			txtDateEnd.setSize(new Dimension(211, 26));
			txtDateEnd.setLocation(new Point(196, 418));
			txtDateEnd.getDateEditor().setEnabled(false);
		}
		return txtDateEnd;
	}

	private JTextField getTxtTime() {
		if (txtTime == null) {
			txtTime = new JTextField();
			txtTime.setLocation(new Point(194, 285));
			txtTime.setSize(new Dimension(211, 26));
		}
		return txtTime;
	}
	
	private void loadDataByID()
	{
		if(panelmanageuser.UserId>=0)
		{
		lbtitle.setText("Update User");
		lbtitle.setSize(new Dimension(140, 28));
		panelTitle.setSize(new Dimension(140, 28));
		btnsave.setIcon(new ImageIcon(getClass().getResource("/image/Save.png")));
		btnsave.setText("Save");
		btnsave.setSize(new Dimension(90, 28));
		ArrayList<FIRModel> fir = FIRDAO.getbyid(panelFIR.IdFIR);
		for(FIRModel model : fir)
		{
			ComboBoxCrimial.setSelectedItem(FIRDAO.getnames("table_NameCrime", model.getIdcriminal()));
			ComboBoxCrimeType.setSelectedItem(FIRDAO.getnames("CRIME_TYPE", model.getIdcrim_Type()));
			if(model.getStatus_crime()==0)
			{
				ComboBoxStatus.setSelectedIndex(1);
				txtDateTemp.setEnabled(false);
				txtDateStart.setEnabled(false);
				txtDateEnd.setEnabled(false);
			}
			else if(model.getStatus_crime()==1)
			{
				ComboBoxStatus.setSelectedIndex(2);
			}else if(model.getStatus_crime()==2)
			{
				ComboBoxStatus.setSelectedIndex(3);
			}
			txtNameTitle.setText(model.getTitle_Name());
			txtTime.setText(String.valueOf(model.getTime()));
			try{
			SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
			if(!model.getDate_Temp().equals("") || model.getDate_Temp()!=null)
				txtDateTemp.setDate(sdf.parse(model.getDate_Temp()));
			if(!model.getDate_Start().equals("") || model.getDate_Start()!=null)
				txtDateStart.setDate(sdf.parse(model.getDate_Start()));
			if(!model.getDate_End().equals("") || model.getDate_End()!=null)
				txtDateTemp.setDate(sdf.parse(model.getDate_End()));
			
			}catch (Exception e) {
				e.printStackTrace();
			}
		
		}
	}
	}


}  
